package phone.qyg.com.qyg;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.base.tool.BaseFragmentActivity;
import com.app.utils.CommonUtil;
import com.app.view.CustomViewPagerAddCache;
import com.baidu.mapapi.SDKInitializer;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import phone.qyg.com.qyg.adapter.FramgmentAdapter;
import phone.qyg.com.qyg.fragment.HomeFragment;
import phone.qyg.com.qyg.fragment.MapFragment;
import phone.qyg.com.qyg.fragment.MyFragment;
import phone.qyg.com.qyg.fragment.TrainFragment;
import phone.qyg.com.qyg.utils.AppUpdateUtil;


@ContentView(R.layout.activity_main)
public class MainActivity  extends BaseFragmentActivity {

    @ViewInject(R.id.home_icon)
    TextView home_icon;

    @ViewInject(R.id.home_title)
    TextView home_title;

    @ViewInject(R.id.map_icon)
    TextView map_icon;

    @ViewInject(R.id.map_title)
    TextView map_title;

    @ViewInject(R.id.act_icon)
    TextView act_icon;

    @ViewInject(R.id.act_title)
    TextView act_title;

    @ViewInject(R.id.train_icon)
    TextView train_icon;

    @ViewInject(R.id.train_title)
    TextView train_title;

    @ViewInject(R.id.supermarket_icon)
    TextView supermarket_icon;

    @ViewInject(R.id.supermarket_title)
    TextView supermarket_title;

    @ViewInject(R.id.my_icon)
    TextView my_icon;

    @ViewInject(R.id.my_title)
    TextView my_title;

    @ViewInject(R.id.home_rela)
    RelativeLayout home_rela;

    @ViewInject(R.id.map_rela)
    RelativeLayout map_rela;

    @ViewInject(R.id.act_rela)
    RelativeLayout act_rela;

    @ViewInject(R.id.train_rela)
    RelativeLayout train_rela;

    @ViewInject(R.id.supermarket_rela)
    RelativeLayout supermarket_rela;

    @ViewInject(R.id.my_rela)
    RelativeLayout my_rela;

    @ViewInject(R.id.home_view_pager)
    CustomViewPagerAddCache home_view_pager;

    MyReciver reciver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        reciver = new MyReciver();
        registerReceiver(reciver,new IntentFilter("selectMy"));
        SDKInitializer.initialize(getApplicationContext());

        x.view().inject(this);

        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
//        ActionBar actionBar = this.getSupportActionBar();
//        actionBar.hide();
//        StatusBarUtil.StatusBarLightMode(this);
        initListFragements();

        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    CommonUtil.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            AppUpdateUtil.appUpdate("",home_icon);
                        }
                    });

                }
            }
        }.start();
    }


    private void initListFragements() {
        List<Fragment> mList = new ArrayList<Fragment>();

        HomeFragment homeFragment = new HomeFragment();
        MapFragment mapFragment = new MapFragment();
//        ActFragment actFragment = new ActFragment();
        TrainFragment trainFragment = new TrainFragment();
//        SuperMarketFragment superMarkeFragment = new SuperMarketFragment();
        MyFragment myFragment = new MyFragment();
        mList.add(homeFragment);
        mList.add(mapFragment);
//        mList.add(actFragment);
        mList.add(trainFragment);
//        mList.add(superMarkeFragment);
        mList.add(myFragment);
        FramgmentAdapter apadter = new FramgmentAdapter(this.getSupportFragmentManager());
        apadter.setFramgmentList(mList);
        home_view_pager.setAdapter(apadter);

        home_icon.setSelected(true);
        home_title.setSelected(true);

    }


    @Event(value = {R.id.home_rela,R.id.map_rela,R.id.act_rela,R.id.train_rela,R.id.supermarket_rela,R.id.my_rela})
    private void homeTitleIconItemOnClick(View v){
//        if(v.getId()==R.id.my_rela){
//            String userInfo = SharePrefUtil.getString(SharePrefUtil.KEY.APP_USER, "");
//            if (!isRequestStr(userInfo)) {
//                //进行拦截 没有登录
//                TransformController.transformController(QXApplication.getContext(), LoginActivity.class);
//                return;
//            }
//        }
        viewPagerItemState();
        switch (v.getId()){
            case R.id.home_rela:
                if (home_view_pager != null)
                    home_view_pager.setCurrentItem(0);
                home_icon.setSelected(true);
                home_title.setSelected(true);
                break;
            case R.id.map_rela:
                if (home_view_pager != null)
                    home_view_pager.setCurrentItem(1);
                map_icon.setSelected(true);
                map_title.setSelected(true);
                break;
//            case R.id.act_rela:
//                if (home_view_pager != null)
//                    home_view_pager.setCurrentItem(2);
//                act_icon.setSelected(true);
//                act_title.setSelected(true);
//                break;
            case R.id.train_rela:
                if(home_view_pager!=null){
                    home_view_pager.setCurrentItem(2);
                }
                train_icon.setSelected(true);
                train_title.setSelected(true);
                break;
//            case R.id.supermarket_rela:
//                if(home_view_pager!=null){
//                    home_view_pager.setCurrentItem(4);
//                }
//                supermarket_icon.setSelected(true);
//                supermarket_title.setSelected(true);
//                break;
            case R.id.my_rela:
                if(home_view_pager!=null){
                    home_view_pager.setCurrentItem(3);
                }
                my_icon.setSelected(true);
                my_title.setSelected(true);
                break;
        }
    }


    /**
     * 改变item状态
     */
    private void viewPagerItemState(){
        home_icon.setSelected(false);
        home_title.setSelected(false);
        map_icon.setSelected(false);
        map_title.setSelected(false);
        act_icon.setSelected(false);
        act_title.setSelected(false);
        train_icon.setSelected(false);
        train_title.setSelected(false);
        supermarket_icon.setSelected(false);
        supermarket_title.setSelected(false);
        my_title.setSelected(false);
        my_icon.setSelected(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(reciver!=null){
            unregisterReceiver(reciver);
        }
    }

    public class MyReciver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            viewPagerItemState();
            if(home_view_pager!=null){
                home_view_pager.setCurrentItem(3);
            }
            my_icon.setSelected(true);
            my_title.setSelected(true);
        }
    }
}
